<?php
/**
 * Created by PhpStorm.
 * User: luoqin
 * Date: 2017/12/27
 * Time: 下午12:12
 */

namespace app\admin\controller;

use app\api\controller\Errorcode;
use think\log;

class Fun
{
    /**
     * @必传值判断 如果$key或者$this->arr有一个为空，则不在判断
     * @param array $wVal 二维数组(0:key,1:条件)
     * @param array $data 传过来的总数据
     * @return array
     */
    static public function will($wVal, $data)
    {
        if (!$wVal) {
            return self::success($data);
        }
        if (!$data) {
            return self::success($data);
        }

        //找出不存的KEY
        $diff = array_diff_key($wVal, $data);
        $key = array_keys($diff);
        if ($key) {
            $keytxt = implode(',', $key);//数组转字符串
            return self::error('999995', '|' . $keytxt);
        }
        //找出集合
        $intersect = array_intersect_key($wVal, $data);
        foreach ($intersect as $k => $v) {
            if ($v['isZero']) {
                if (empty($data[$k])) {
                    return self::error('999994', '|' . $k);
                }
            } else {
                if ($data[$k] == "") {
                    return self::error('999993', '|' . $k);
                }
            }
        }
        return self::success($data);
    }

    /**
     * @成功返回
     * @param $data
     * @param $txt
     * @return array
     */
    static public function success($data, $txt = '')
    {
        $out['code'] = '000000';
        $out['msg'] = empty($txt) ? '操作成功' : $txt;
        $out['data'] = $data;
        return $out;
    }

    /**
     * @错误返回
     * @param string $code
     * @return array
     */
    static public function error($code, $txt = '', $type = 0)
    {
        if (isset(Errorcode::$code[$code])) {
            $out['code'] = $code;
            if ($type == 0) {
                $out['msg'] = Errorcode::$code[$code] . $txt;
            } else {
                $out['msg'] = $txt;
            }
        } else {
            $out['code'] = '999999';
            $out['msg'] = Errorcode::$code[$out['code']] . ',未定义' . $code . '的错误说明';
        }
        $out['data'] = [];
        return $out;
    }

    /**
     * @文件日志
     * @param string $data
     * @param string $desc
     */
    static public function LOG($data = "", $desc = "", $type = "log")
    {
        Log::write($desc . $data, $type, true);
    }

    //生成csv文件
    static public function exportCsv($file_name, $header, $data)
    {
        // 打开文件资源，不存在则创建
        $fp = fopen(mb_convert_encoding($file_name, 'gb2312', 'utf-8'), 'a');
        // 处理头部标题
        $header = implode(',', $header) . PHP_EOL;
        // 处理内容
        $content = '';
        foreach ($data as $k => $v) {
            $content .= implode(',', $v) . PHP_EOL;
        }
        // 拼接
        $csv = $header . $content;
        $csv = mb_convert_encoding($csv, 'gb2312', 'utf-8');
        // 写入并关闭资源
        fwrite($fp, $csv);
        fclose($fp);
    }

    //后台密码加密
    static public function encryptPwd($pwd)
    {
        return $pwd;
    }
}